
import {
    SET_USERINFO,
    ADD_PAGE,
    LOAD_MORE,
    ADD_NUM,
    SUB_NUM,
    INIT_NUM,
    GET_TOTAL,
    REMOVE_USERINFO
} from "../type"

let initData = {
    userinfo: {},
    postlist: [],
    page: 15,
    total: 0,
    num: 1
}

function indexReducer(state = initData, action) {
    state = JSON.parse(JSON.stringify(state));
    switch (action.type) {
        case GET_TOTAL:
             state.total = action.total;
             return state;
        case ADD_NUM:
            if(state.num<state.total){
                state.num++;
            }
            return state;
        case SUB_NUM:
            if(state.num!==1){
                state.num--;
            }
            return state;
        case INIT_NUM:
             state.num = 1;
             return state;
        case SET_USERINFO:
            state.userinfo = action.userinfo;
            return state;
        case REMOVE_USERINFO:
            state.userinfo = {};
            return state;    
        case ADD_PAGE:
            state.page += 15;
            return state;
        case LOAD_MORE:
            state.postlist = [...state.postlist, ...action.data];
            return state;
        default:
            return state;
    }
}

export default indexReducer;